
'''
这个模块可以呈现gis过程图形，展示用
'''

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

import shapely
import shapely.wkt
from descartes import PolygonPatch
import autonei.geometry as geo

def plot_voronoi():
    '''生成邻区，并呈现'''

    cellinfo=pd.read_csv(r'C:\Users\juyhe\Documents\JupyterNoteBook\data\cellinfo.csv',encoding='gbk')
    voronoinei = geo.VoronoiNRs4G(cellinfo)
    nrs = voronoinei.nrs
    polygons = voronoinei.siteinfo

    #只要有图形的
    polygons_plot = polygons[(polygons['polygons'] != 'None') & (polygons['polygons'] != '')]

    s_enbid=input(r'选一个测试区域的宏站的enbid,建议"32895":')
    if not s_enbid:
        s_enbid=32895

    s_enbid=int(s_enbid)

    #挑选一个站点多边形
    s_site = polygons_plot['polygons'][polygons_plot['enbid'] == s_enbid]
    s_site_geo = s_site.values[0]  # dataframe这样切片出来的是series,需要转换一下

    #所挑选的站点的邻站
    adj_df = nrs[nrs['siteid_a'] == s_enbid]['siteid_b'].values
    adj_df = list(adj_df)
    adj_geo = []
    for index, row in polygons_plot.iterrows():
        if row['enbid'] in adj_df:
            adj_geo.append(row['polygons'])
    adj_geo = geo.jsonlist2jsondict(adj_geo)

    #所有测试站点
    adj_all=polygons_plot.polygons
    adj_all=geo.jsonlist2jsondict(adj_all)
    x = polygons_plot.longitude
    y = polygons_plot.latitude
    t = polygons_plot.enbid

    #通过matplotlib呈现
    fig, ax = plt.subplots()
    ax.add_patch(PolygonPatch(s_site_geo, fc='b', ec='m', alpha=0.5, zorder=3))
    ax.add_patch(PolygonPatch(adj_geo, fc='y', ec='m', alpha=1, zorder=2, lw=0.5))
    ax.add_patch(PolygonPatch(adj_all, fc='w', ec='m', alpha=1, zorder=1, lw=2))
    plt.plot(x, y, 'b.')
    ax.axis('scaled')
    plt.xlim(121.590, 121.620)
    plt.ylim(31.180, 31.210)
    plt.show()


def main():

    plot_voronoi()
    pass


if __name__ == '__main__':
    main()